//
//  HXTabBar.m
//  百思不得姐
//
//  Created by Hubery on 16/1/13.
//  Copyright © 2016年 Whx. All rights reserved.
//

#import "HXTabBar.h"
#import "XMGPublishViewView.h"
#import "XMGPublishWindowView.h"
#import "XMGPublishViewController.h"

@interface HXTabBar()
@property(nonatomic,weak) UIButton *publishBtn;
@end

@implementation HXTabBar


/**
 代码重写控件调用这个方法
 这里的 fram 是假的  要在layoutSubviews 布局
 */
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        UIButton *publishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [publishBtn setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [publishBtn addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];
        [publishBtn setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
        [self addSubview:publishBtn];
        self.publishBtn = publishBtn;
    }
    return self;
}


////控制器实现了发布界面
- (void)publishClick
{
    XMGPublishViewController *publish = [[XMGPublishViewController alloc] init];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:publish animated:NO completion:nil];
}


//View实现了发布   半透明 view可控制器view互不影响
//- (void)publishClick
//{
//    XMGPublishViewView *publish = [XMGPublishViewView publishView];
//    UIWindow *window = [UIApplication sharedApplication].keyWindow;
//    publish.frame = window.bounds;
//    [window addSubview:publish];
//}


//新建window的发布

//UIWindow *window;
//UIWindow *window2;

//- (void)publishClick
//{
//    // 窗口级别 可以实现永远显示在屏幕的某个位置
//    // UIWindowLevelNormal < UIWindowLevelStatusBar < UIWindowLevelAlert
//    
//    //    window = [[UIWindow alloc] init];
//    //    window.frame = CGRectMake(0, 0, 375, 20);
//    //    window.backgroundColor = [UIColor yellowColor];
//    //    window.windowLevel = UIWindowLevelStatusBar;
//    //    window.hidden = NO;//他不需要加到某个什么上面 只需要取消隐藏就可以
//    
//    //    window2 = [[UIWindow alloc] init];
//    //    window2.frame = CGRectMake(100, 100, 100, 100);
//    //    window2.backgroundColor = [UIColor redColor];
//    //    window2.hidden = NO;
//    
//    [XMGPublishWindowView show];
//}


- (void)layoutSubviews{
    // 标记按钮是否已经添加过监听器
    static BOOL added = NO;
    HXLogFunc;//调用了哪个类的哪个方法
    [super layoutSubviews];
    //拿到背景图片的大小
    self.publishBtn.bounds = CGRectMake(0, 0, self.publishBtn.currentBackgroundImage.size.width, self.publishBtn.currentBackgroundImage.size.height);
    self.publishBtn.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
    // 设置其他UITabBarButton的frame
    CGFloat buttonY = 0;
    CGFloat buttonW = self.frame.size.width / 5;
    CGFloat buttonH = self.frame.size.height;
    NSInteger index = 0;
    for (UIControl *btn in self.subviews) {
        if (![btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue;
        //继承自UIControl
        //if (![btn isKindOfClass:[UIControl class]] || btn == self.publishBtn) continue;
        // 计算按钮的x值
        CGFloat buttonX = buttonW * ((index > 1)?(index + 1):index);
        btn.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        // 增加索引
        index++;
        
        if (added == NO) {
            // 监听按钮点击
            [btn addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
        }
    }
}

- (void)buttonClick
{
    // 发出一个通知
//    [XMGNoteCenter postNotificationName:XMGTabBarDidSelectNotification object:nil userInfo:nil];
    //已经采用了成为tabbar代理的方式监听了重复点击
}

@end
